#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_pwm.h"
#include "iot_i2c.h"
#include "iot_errno.h"

#include "ssd1306.h"

#include "hi_io.h"

#define OLED_I2C_BAUDRATE (400*1000)

//正在排队人数
extern int ZTKD;
extern int YTKD;
extern int SFKD;

//在屏幕上显示排队人数
void ssd1306_reportnum(void) {
    uint32_t t = 1000;

    ssd1306_Fill(Black);
    ssd1306_SetCursor(0, 0);
    ssd1306_DrawString("In sorting......", Font_7x10, White);

    
    char numStr1[10];
    char numStr2[10];
    char numStr3[10];
    
    // 显示中通快递数量
    ssd1306_SetCursor(0, 20);
    if(ZTKD > 0){
        sprintf(numStr1, "%d", ZTKD);   //将整型数字转化为字符串，从而可以在屏幕上显示
        ssd1306_DrawString(numStr1, Font_7x10, White);

        ssd1306_SetCursor(strlen(numStr1)*7, 20);
        ssd1306_DrawString(" Zhongtong Pack", Font_7x10, White);
    }
    else{
        ssd1306_DrawString("NO", Font_7x10, White);

        ssd1306_SetCursor(strlen("NO")*7, 20);
        ssd1306_DrawString(" Zhongtong Pack", Font_7x10, White);
    }
    // 显示圆通快递数量
    ssd1306_SetCursor(0, 35);
    if(YTKD > 0){
        sprintf(numStr2, "%d", YTKD);   //将整型数字转化为字符串，从而可以在屏幕上显示
        ssd1306_DrawString(numStr2, Font_7x10, White);

        ssd1306_SetCursor(strlen(numStr2)*7, 35);
        ssd1306_DrawString(" Yuantong Pack", Font_7x10, White);
    }
    else{
        ssd1306_DrawString("NO", Font_7x10, White);

        ssd1306_SetCursor(strlen("NO")*7, 35);
        ssd1306_DrawString(" Yuantong Pack", Font_7x10, White);
    }
    // 显示顺丰快递数量
    ssd1306_SetCursor(0, 50);
    if(SFKD > 0){
        sprintf(numStr3, "%d", SFKD);   //将整型数字转化为字符串，从而可以在屏幕上显示
        ssd1306_DrawString(numStr3, Font_7x10, White);

        ssd1306_SetCursor(strlen(numStr3)*7, 50);
        ssd1306_DrawString(" Shunfeng Pack", Font_7x10, White);
    }
    else{
        ssd1306_DrawString("NO", Font_7x10, White);

        ssd1306_SetCursor(strlen("NO")*7, 50);
        ssd1306_DrawString(" Shunfeng Pack", Font_7x10, White);
    }

    //刷新屏幕
    ssd1306_UpdateScreen();
    HAL_Delay(t);
}

void Ssd1306TestTask(void* arg)
{
    (void) arg;
    IoTGpioInit(HI_IO_NAME_GPIO_13);
    IoTGpioInit(HI_IO_NAME_GPIO_14);

    hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
    hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);
    
    IoTI2cInit(0, OLED_I2C_BAUDRATE);

    // WatchDogDisable();

    usleep(20*1000);
    ssd1306_Init();
    ssd1306_Fill(Black);

    usleep(2000*1000);

    uint32_t start = HAL_GetTick();
    ssd1306_UpdateScreen();
    uint32_t end = HAL_GetTick();
    printf("ssd1306_UpdateScreen time cost: %d ms.\r\n", end - start);

    while (1) {
        ssd1306_reportnum();
    }
}

void Ssd1306TestDemo(void)
{
    osThreadAttr_t attr;

    attr.name = "Ssd1306Task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 10240;
    attr.priority = osPriorityNormal;

    if (osThreadNew(Ssd1306TestTask, NULL, &attr) == NULL) {
        printf("[Ssd1306TestDemo] Falied to create Ssd1306TestTask!\n");
    }
}

APP_FEATURE_INIT(Ssd1306TestDemo);